<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model.trim="formData.username" style="width: 75%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model.trim="formData.mobile" style="width: 75%" placeholder="请输入手机" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width: 75%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width: 75%" placeholder="请选择聘用形式">
          <el-option v-for="item in Employees.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width: 75%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 75%"
          placeholder="请选择部门"
          @focus="getDepartments"
        />
        <!-- <el-cascader
          v-model="formData.departmentName"
          :options="treeData"
          :props="{ expandTrigger: 'hover', value: 'name', label: 'name', emitPath: false, checkStrictly: true }"
          style="width: 75%"
          size="small"
          :show-all-levels="false"
          placeholder="请选择部门"
          @focus="getDepartments"
        /> -->
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width: 75%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <el-row type="flex" align="middle" justify="center">
      <el-col :span="8">
        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="btnOk">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate } from '@/utils'
import Employees from '@/api/constant/employees'

export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      Employees,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, trigger: 'blur', message: '姓名不能为空' },
          { min: 1, max: 4, trigger: 'blur', message: '姓名为1-4位' }
        ],
        mobile: [
          { required: true, trigger: 'blur', message: '手机号不能为空' },
          {
            pattern: /^1[3-9]\d{9}$/,
            trigger: 'blur',
            message: '手机号格式不正确'
          }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'change' }],
        workNumber: [{ required: true, trigger: 'blur', message: '工号不能为空' }],
        departmentName: [{ required: true, trigger: 'change', message: '部门不能为空' }],
        timeOfEntry: [{ required: true, trigger: 'blur', message: '请选择入职时间' }]
      },
      treeData: []
    }
  },
  computed: {},
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      this.treeData = tranListToTreeDate(depts, '')
    },
    btnCancel() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.formData.resetFields()
      this.$emit('update:showDialog', false)
    },
    async btnOk() {
      try {
        await this.$refs.formData.validate()
        await addEmployee(this.formData)
        // this.$emit('update:showDialog', false)
        this.$parent.getEmployeeList && this.$parent.getEmployeeList()
        this.$parent.showDialog = false
        this.$message.success('添加员工成功！')
      } catch (error) {
        this.$message.danger('添加员工失败！')
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog {
  margin-top: 50px !important;
  .el-dialog__header {
    background-color: rgba(38, 24, 230, 0.726);
    .el-dialog__title,
    .el-dialog__close {
      color: #fff;
    }
  }
}
// .el-popper {
//   background-color: blueviolet;
//   .el-cascader-menu {
//     max-width: 250px;
//     background-color: rgb(82, 226, 15);
//   }
// }
</style>
